package server;

import java.net.InetAddress;

public class NodeMange {
	void RemoveNode(int sessionId){
		Config.nodeTable.remove(sessionId);
	}
	
	Node getNode(int sessionId){
		return Config.nodeTable.get(sessionId);
	}
	
	boolean contiansNode(int sessionId){
		return Config.nodeTable.containsKey(sessionId);
	}
	
	void addNode(InetAddress nodeIp,int sessionId,short nodeType,int routPort,int DBServerPort,int AssistServerPort){
		Node node=new Node(nodeIp,sessionId,nodeType,routPort,AssistServerPort,DBServerPort);
		Config.nodeTable.put(sessionId, node);
		//Config.println("ADD NODE "+(Config.nodeTable.get(sessionId)).sessionId+" NodeType "+node.nodeType+" DBServerPort "+DBServerPort+" AssistServerPort "+AssistServerPort);
	}
	
	Node getRandomSNode(){
		Node node=null;;
		if(Config.sNodeList.size()!=0&Config.sNodeTable.size()!=0){
			for(int i=0;i<3;i++){
				int j;
				j=peer.tools.RandomNum.randomInt(Config.sNodeList.size());
				int sessionId=Config.sNodeList.get(j);
				node=Config.nodeTable.get(sessionId);
				if(node!=null){
					break;
				}
			}
		}
		return node;
	}
}
